package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.DataFlg;
import com.isunland.managesystem.entity.Query;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.Date;

/**
 * 考勤汇总按照时间或人员筛选
 *
 * @author wu
 */
public class ReceiptQueryFragment extends Fragment implements OnClickListener {
  public static final String EXTRA_QUERY = "com.isunland.managesystem.ui.EXTRA_QUERY";
  private static final int REQUEST_STARTTIME = 0;
  private static final int REQUEST_ENDTIME = 1;
  private static final int REQUEST_DATA_STATUS = 2;
  private static final int REQUEST_REGISTER_TIME = 3;
  private LinearLayout mDataStatusVg;
  private TextView mDataStatusTv;

  private LinearLayout mRegisterTimeVg;
  private TextView mRegisterTimeTv;

  private LinearLayout mReceiptStartTimeVg;
  private TextView mReceiptStartTimeTv;

  private LinearLayout mReceiptEndTimeVg;
  private TextView mReceiptEndTimeTv;

  private BaseVolleyActivity mActicity;
  private Query mQuery;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.sift);

    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_query_receipt, container, false);
    init(view);

    return view;
  }

  private void init(View v) {
    mActicity = (BaseVolleyActivity) getActivity();
    mQuery = (Query) getArguments().getSerializable(EXTRA_QUERY);
    if (mQuery == null) {
      mQuery = new Query();
    }
    mDataStatusVg = (LinearLayout) v.findViewById(R.id.ll_dataStatus_query);
    mReceiptStartTimeVg = (LinearLayout) v.findViewById(R.id.ll_receiptStartTime_query);
    mReceiptEndTimeVg = (LinearLayout) v.findViewById(R.id.ll_receiptEndTime_query);
    mRegisterTimeVg = (LinearLayout) v.findViewById(R.id.ll_registerTime_query);
    mDataStatusTv = (TextView) v.findViewById(R.id.tv_dataStatus_query);
    mRegisterTimeTv = (TextView) v.findViewById(R.id.tv_registerTime_query);
    mReceiptStartTimeTv = (TextView) v.findViewById(R.id.tv_receiptStartTime_query);

    mReceiptEndTimeTv = (TextView) v.findViewById(R.id.tv_receiptEndTime_query);

    mDataStatusTv.setText(mQuery.getDataStatusValue());
    mRegisterTimeTv.setText(mQuery.getRegisterTimeValue());
    mReceiptStartTimeTv.setText(mQuery.getReceiptStartTime());
    mReceiptEndTimeTv.setText(mQuery.getReceiptEndTime());

    mDataStatusVg.setOnClickListener(this);
    mRegisterTimeVg.setOnClickListener(this);
    mReceiptStartTimeVg.setOnClickListener(this);
    mReceiptEndTimeVg.setOnClickListener(this);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ll_dataStatus_query:
        showDialog(REQUEST_DATA_STATUS);
        break;
      case R.id.ll_registerTime_query:
        showDialog(REQUEST_REGISTER_TIME);
        break;
      case R.id.ll_receiptStartTime_query:
        showDialog(REQUEST_STARTTIME);
        break;
      case R.id.ll_receiptEndTime_query:
        showDialog(REQUEST_ENDTIME);
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_STARTTIME:
        dialog =
            BaseTimeDialogFragment.newInstance(DateUtil.stringToDate(mQuery.getReceiptStartTime()));
        break;
      case REQUEST_ENDTIME:
        dialog =
            BaseTimeDialogFragment.newInstance(DateUtil.stringToDate(mQuery.getReceiptEndTime()));
        break;
      case REQUEST_REGISTER_TIME:
        dialog = DataDictDialogFragment.newInstance(DataFlg.REGISTER_TIME);
        break;

      case REQUEST_DATA_STATUS:
        dialog = DataDictDialogFragment.newInstance(DataFlg.DATA_STATUS);
        break;
      default:
        ToastUtil.show("显示对话框错误");
        return;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  public static Fragment newInstance(Query query) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_QUERY, query);
    ReceiptQueryFragment fragment = new ReceiptQueryFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮

      case android.R.id.home:
        //			ToastUtil.show("返回");
        getActivity().finish();
        break;

      case R.id.menu_item_confirm:
        //			ToastUtil.show("确定");
        Intent result = new Intent();
        result.putExtra(EXTRA_QUERY, mQuery);
        getActivity().setResult(Activity.RESULT_OK, result);
        getActivity().finish();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    switch (requestCode) {
      case REQUEST_STARTTIME:
        String startTime = getDate(data);
        mQuery.setReceiptStartTime(startTime);
        mReceiptStartTimeTv.setText(startTime);
        break;
      case REQUEST_ENDTIME:
        String endTime = getDate(data);
        mQuery.setReceiptEndTime(endTime);
        mReceiptEndTimeTv.setText(endTime);
        break;
      case REQUEST_REGISTER_TIME:
        CustomerDialog registerTime =
            (CustomerDialog) data.getSerializableExtra(CustomerDialogFragment.EXTRA_VALUE);
        mQuery.setRegisterTimeValue(registerTime.getName());
        mQuery.setRegisterTimeKey(registerTime.getId());
        mRegisterTimeTv.setText(registerTime.getName());
        break;

      case REQUEST_DATA_STATUS:
        CustomerDialog dataStatus =
            (CustomerDialog) data.getSerializableExtra(CustomerDialogFragment.EXTRA_VALUE);
        mQuery.setDataStatusKey(dataStatus.getId());
        mQuery.setDataStatusValue(dataStatus.getName());
        mDataStatusTv.setText(dataStatus.getName());
        break;
      default:
        ToastUtil.show("显示对话框错误");
        return;
    }
  }

  /**
   * @param data
   * @return
   */
  private String getDate(Intent data) {
    Date result = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
    String dateString = DateUtil.dateToString(result, DateUtil.TYPE_DATE);
    return dateString;
  }
}
